package com.micangcaihang.micang.view;

import android.app.Activity;
import android.graphics.Color;
import android.view.View;

import com.afollestad.materialdialogs.MaterialDialog;
import com.micangcaihang.common.widget.pickerview.OptionsPickerView;
import com.micangcaihang.common.widget.pickerview.TimePickerView;
import com.micangcaihang.micang.R;
import com.micangcaihang.micang.entity.PickerEntity;
import com.micangcaihang.micang.entity.ProvinceEntity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/08/03
 *     update : 2017/08/03
 *     desc   : 条件选择器综合
 *     version: 1.0
 * </pre>
 */

public class PickerViewHelper {

    public static void showAmountpinion(Activity activity,
                                        String room, String hall, String toilet, String kitchen,
                                        OnAmountSelectListener onAmountSelectListener) {
        int posRoom = 0;
        int posHall = 0;
        int posKitchen = 0;
        int posToilet = 0;
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            String s = i + "";
            list.add(s);
            if (s.equals(room)) {
                posRoom = i;
            }
            if (s.equals(hall)) {
                posHall = i;
            }
            if (s.equals(kitchen)) {
                posKitchen = i;
            }
            if (s.equals(toilet)) {
                posToilet = i;
            }
        }
        OptionsPickerView pickerView = new OptionsPickerView.Builder(activity, new OptionsPickerView.OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int options2, int options3, int option4, View v) {
                onAmountSelectListener.onAmountSelect(list.get(options1), list.get(options2), list.get(options3), list.get(option4));
            }
        }).setOutSideCancelable(true)
                .setTitleText("房屋户型")
                .setLabels("室", "厅", "卫", "厨")
                .setCyclic(false, false, false, false)
                .isCenterLabel(false)
                .isDialog(true)
                .setDividerColor(Color.parseColor("#FF9E00"))
                .build();
        pickerView.setSelectOptions(posRoom, posHall, posKitchen, posToilet);
        pickerView.setNPicker(list, list, list, list);
        pickerView.show();
    }

    public interface OnAmountSelectListener {
        void onAmountSelect(String room, String hall, String toilet, String kitchen);
    }

    public static void showTwoPickerView(Activity activity, String title, String listId1, String listId2,
                                         List<PickerEntity> list1, List<PickerEntity> list2,
                                         OnTwoSelectListener onTwoSelectListener) {
        int option1 = 0;
        int option2 = 0;
        for (int i = 0; i < list1.size(); i++) {
            PickerEntity entity = list1.get(i);
            if (entity.getSign_id().equals(listId1)) {
                option1 = i;
            }
        }
        for (int i = 0; i < list2.size(); i++) {
            PickerEntity entity = list2.get(i);
            if (entity.getSign_id().equals(listId2)) {
                option2 = i;
            }
        }
        OptionsPickerView pickerView = new OptionsPickerView.Builder(activity, new OptionsPickerView.OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int options2, int options3, int options4, View v) {
                onTwoSelectListener.OnTwoSelect(list1.get(options1), list2.get(options2));
            }
        }).setOutSideCancelable(true)
                .setTitleText(title)
                .setCyclic(false, false, false, false)
                .isCenterLabel(false)
                .isDialog(true)
                .setDividerColor(Color.parseColor("#FF9E00"))
                .build();
        pickerView.setSelectOptions(option1, option2);
        pickerView.setNPicker(list1, list2, null, null);
        pickerView.show();
    }

    public interface OnTwoSelectListener {
        void OnTwoSelect(PickerEntity pickerEntity1, PickerEntity pickerEntity2);
    }

    public static void showOnePickerView(Activity activity, String title, String listId1,
                                         List<PickerEntity> list1,
                                         OnOneSelectListener onOneSelectListener) {
        int option1 = 0;
        for (int i = 0; i < list1.size(); i++) {
            PickerEntity entity = list1.get(i);
            if (entity.getSign_id().equals(listId1)) {
                option1 = i;
            }
        }
        OptionsPickerView pickerView = new OptionsPickerView.Builder(activity, new OptionsPickerView.OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int options2, int options3, int options4, View v) {
                onOneSelectListener.OnOneSelect(list1.get(options1));
            }
        }).setOutSideCancelable(true)
                .setTitleText(title)
                .setCyclic(false, false, false, false)
                .isCenterLabel(false)
                .isDialog(true)
                .setDividerColor(Color.parseColor("#FF9E00"))
                .build();
        pickerView.setSelectOptions(option1);
        pickerView.setNPicker(list1, null, null, null);
        pickerView.show();
    }

    public interface OnOneSelectListener {
        void OnOneSelect(PickerEntity pickerEntity1);
    }

    /**
     * 显示年月日pickerview
     *
     * @param activity
     * @param startDate 开始时间
     * @param endDate   结束时间
     */
    public static void showYearMonthDayPicker(Activity activity, String title, Calendar startDate, Calendar endDate,
                                              Calendar selectedDate,
                                              OnTimeSelectListenner onTimeSelectListenner) {
        TimePickerView pickerView = new TimePickerView.Builder(activity, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                onTimeSelectListenner.onTimeSelect(date);
            }
        }).setOutSideCancelable(true)
                .setTitleText(title)
                .isCenterLabel(false)
                .setDate(selectedDate).isDialog(true)
                .setRangDate(startDate, endDate)
                .setType(new boolean[]{true, true, true, false, false, false})
                .setDividerColor(Color.parseColor("#FF9E00"))
                .build();
        pickerView.show();
    }

    public interface OnTimeSelectListenner {
        void onTimeSelect(Date date);
    }

    public static void showMultiItem(Activity activity, String title,
                                     List<String> list, Integer[] integers,
                                     OnMultiChoiceListenner onMultiChoiceListenner) {
        new MaterialDialog.Builder(activity)
                .title(title)
                .items(list)
                .onPositive((dialog, which) -> dialog.dismiss())
                .onNegative((dialog, which) -> dialog.dismiss())
                .itemsCallbackMultiChoice(integers, (dialog, which, text) -> {
                    onMultiChoiceListenner.onMultiChoice(dialog, which, text);
                    return true;
                })
                .positiveText("确定")
                .negativeText("取消")
                .autoDismiss(true)
                .show();
    }

    public interface OnMultiChoiceListenner {
        void onMultiChoice(MaterialDialog dialog, Integer[] which, CharSequence[] text);
    }

    public static void showProvinceThree(Activity activity, String title, List<ProvinceEntity> provinceEntities,
                                         String provinceEntityCode, String cityBeanCode,
                                         String areaBeanCode, OnSelectProvinceListenner onSelectProvinceListenner) {
        List<ProvinceEntity> mOptions1Items;
        List<ArrayList<ProvinceEntity.CityBean>> mOptions2Items = new ArrayList<>();
        List<ArrayList<ArrayList<ProvinceEntity.CityBean.AreaBean>>> mOptions3Items = new ArrayList<>();
        int option1 = 0;
        int option2 = 0;
        int option3 = 0;
        mOptions1Items = provinceEntities;

        for (int i = 0; i < provinceEntities.size(); i++) {//遍历省份
            if (provinceEntityCode.equals(provinceEntities.get(i).getCode())) {
                option1 = i;
            }
            ArrayList<ProvinceEntity.CityBean> CityList = new ArrayList<>();//该省的城市列表（第二级）
            ArrayList<ArrayList<ProvinceEntity.CityBean.AreaBean>> Province_AreaList = new ArrayList<>();//该省的所有地区列表（第三极）

            for (int c = 0; c < provinceEntities.get(i).getCity().size(); c++) {//遍历该省份的所有城市
                ProvinceEntity.CityBean CityName = provinceEntities.get(i).getCity().get(c);
                if (cityBeanCode.equals(CityName.getCode())) {
                    option2 = c;
                }
                CityList.add(CityName);//添加城市

                ArrayList<ProvinceEntity.CityBean.AreaBean> City_AreaList = new ArrayList<>();//该城市的所有地区列表

                //如果无地区数据，建议添加空字符串，防止数据为null 导致三个选项长度不匹配造成崩溃
                if (provinceEntities.get(i).getCity().get(c).getArea() == null
                        || provinceEntities.get(i).getCity().get(c).getArea().size() == 0) {
                    City_AreaList.add(new ProvinceEntity.CityBean.AreaBean("", ""));
                } else {

                    for (int d = 0; d < provinceEntities.get(i).getCity().get(c).getArea().size(); d++) {//该城市对应地区所有数据
                        ProvinceEntity.CityBean.AreaBean AreaName = provinceEntities.get(i).getCity().get(c).getArea().get(d);
                        City_AreaList.add(AreaName);//添加该城市所有地区数据
                        if (areaBeanCode.equals(AreaName.getCode())) {
                            option3 = c;
                        }
                    }
                }
                Province_AreaList.add(City_AreaList);//添加该省所有地区数据
            }

            /**
             * 添加城市数据
             */
            mOptions2Items.add(CityList);

            /**
             * 添加地区数据
             */
            mOptions3Items.add(Province_AreaList);

        }
        List<ProvinceEntity> finalMOptions1Items = mOptions1Items;
        OptionsPickerView pvOptions = new OptionsPickerView.Builder(activity, (options1, options2, options3, option4, v) -> {
            //返回的分别是三个级别的选中位置
            onSelectProvinceListenner.OnSelectProvince(finalMOptions1Items.get(options1),
                    mOptions2Items.get(options1).get(options2),
                    mOptions3Items.get(options1).get(options2).get(options3));
        })
                .setOutSideCancelable(true)
                .setTitleText(title)
                .setDividerColor(Color.parseColor("#FF9E00"))
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(20)
                .isDialog(true)
                .setSelectOptions(option1, option2, option3)
                .build();
        pvOptions.setPicker(mOptions1Items, mOptions2Items, mOptions3Items);//三级选择器
        pvOptions.show();
    }

    public interface OnSelectProvinceListenner {
        void OnSelectProvince(ProvinceEntity provinceEntity, ProvinceEntity.CityBean cityBean, ProvinceEntity.CityBean.AreaBean areaBean);
    }

    public static void showCameraList(Activity activity, CameraListItemClickListenner cameraListItemClickListenner) {
        new MaterialDialog.Builder(activity)
                .items(R.array.cameralist)
                .itemsCallback((dialog, view, which, text) -> cameraListItemClickListenner.onCameraListItemClick(which))
                .show();
    }

    public static void showAudioList(Activity activity, CameraListItemClickListenner cameraListItemClickListenner) {
        new MaterialDialog.Builder(activity)
                .items(R.array.audiolist)
                .itemsCallback((dialog, view, which, text) -> cameraListItemClickListenner.onCameraListItemClick(which))
                .show();
    }

    public interface CameraListItemClickListenner {
        void onCameraListItemClick(int which);
    }
}
